#!/bin/sh

I2C_BUS=${I2C_BUS:-15}
I2C_SA=${I2C_SA:-0x39}

usage() {
	echo "ufm CMD"
	echo ""
	echo "CMD List:"
	echo "  provision  - Deploy default root key and offsets"
	echo "  lock       - Lock UFM"
	echo "  erase      - Erase UFM buffer if not locked"
}

do_provision() {
	echo "Provision Root Key i2c-${I2C_BUS}[${I2C_SA}]"
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0d \
	             0x6b 0xa4 0xa6 0x98 0x53 0x63 0xf0 0xe7 \
	             0xe9 0x98 0x76 0x62 0x7d 0xe7 0x12 0x41 \
	             0xda 0xab 0x4b 0x96 0xbd 0x67 0x99 0x82 \
	             0x81 0x40 0x27 0x87 0xa5 0x10 0x6e 0x73 \
	             i

	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0b 0x01 0x03 i

	echo "Provision PCH Offset i2c-${I2C_BUS}[${I2C_SA}]"
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0d \
	             0x00 0x80 0xFD 0x03 \
	             0x00 0x80 0xFD 0x02 \
	             0x00 0x80 0xFD 0x01 \
	             i
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0b 0x05 0x03 i

	echo "Provision BMC Offset i2c-${I2C_BUS}[${I2C_SA}]"
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0d \
	             0x00 0x00 0x0e 0x00 \
	             0x00 0x00 0xa0 0x02 \
	             0x00 0x00 0xa0 0x04 \
	             i
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0b 0x06 0x03 i
}

do_lock() {
	echo "Lock UFM i2c-${I2C_BUS}[${I2C_SA}]"
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0b 0x07 0x01 i
}

do_erase() {
	echo "Erase UFM i2c-${I2C_BUS}[${I2C_SA}]"
	i2cset -y ${I2C_BUS} ${I2C_SA} 0x0b 0x00 0x01 i
}

if [ $# -lt 1 ]
then
	usage
	exit 1
fi

subcmd=$1
shift

case $subcmd in
	provision)
		do_provision
		;;
	lock)
		do_lock
		;;
	erase)
		do_erase
		;;
	-h|--help|*)
		usage
		;;
esac
